package cn.edu.seu.cose.qol.util;

import java.io.PrintStream;
import java.util.Date;

public class Logger {
	
	private static final String LOG_TYPE_INFO = "INFO";
	
	private static final String LOG_TYPE_DEBUG = "DEBUG";
	
	private static final String LOG_TYPE_WARN = "WARN";
	
	private static final String LOG_TYPE_ERROR = "ERROR";
	
	private PrintStream _out;
	
	public Logger(PrintStream out) {
		_out = out;
	}
	
	public void info(Object msg) {
		printLog(LOG_TYPE_INFO, msg, null);
	}
	
	public void info(Object msg, Throwable t) {
		printLog(LOG_TYPE_INFO, msg, t);
	}
	
	public void debug(Object msg) {
		printLog(LOG_TYPE_DEBUG, msg, null);
	}
	
	public void debug(Object msg, Throwable t) {
		printLog(LOG_TYPE_DEBUG, msg, t);
	}
	
	public void warn(Object msg) {
		printLog(LOG_TYPE_WARN, msg, null);
	}
	
	public void warn(Object msg, Throwable t) {
		printLog(LOG_TYPE_WARN, msg, t);
	}
	
	public void error(Object msg) {
		printLog(LOG_TYPE_ERROR, msg, null);
	}
	
	public void error(Object msg, Throwable t) {
		printLog(LOG_TYPE_ERROR, msg, t);
	}
	
	private void printLog(String logType, Object msg, Throwable t) {
		_out.println(logType + "\t" + new Date());
		_out.println(msg);
		
		if (t != null) {
			t.printStackTrace(_out);
		}
		
		_out.println();
	}

}
